package com.xinya.wanandroidclient.eventbus.observer

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.xinya.wanandroidclient.eventbus.bean.LogoutMessage
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 *
 * @ClassName:      LogoutMessageObserver$
 * @Description:    java类作用描述
 * @Author:         xinya
 * @CreateDate:     2021/3/11$ 15:20$
 * @UpdateRemark:   更新内容
 * @Version:        1.0
 */
class LogoutMessageObserver : LifecycleObserver {

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun start() {
        EventBus.getDefault().register(this)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun stop() {
        EventBus.getDefault().unregister(this)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onLogoutMessageEvent(message: LogoutMessage) {
        messageEvent?.invoke(message)
    }

    private var messageEvent: ((LogoutMessage) -> Unit)? = null
    fun setMessageEvent(event: (LogoutMessage) -> Unit) {
        this.messageEvent = event
    }
}